package com.cleaningcloud.cld.service.impl;

import com.cleaningcloud.cld.domain.CldContract;
import com.cleaningcloud.cld.domain.CldProject;
import com.cleaningcloud.cld.mapper.CldContractMapper;
import com.cleaningcloud.cld.mapper.CldProjectMapper;
import com.cleaningcloud.cld.service.ICldContractService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.redis.service.RedisService;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合同Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-10-06
 */
@Service
public class CldContractServiceImpl implements ICldContractService {
    @Autowired
    private CldContractMapper cldContractMapper;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CldProjectMapper cldProjectMapper;

    /**
     * 查询合同
     *
     * @param contractId 合同主键
     * @return 合同
     */
    @Override
    public CldContract selectCldContractByContractId(Long contractId) {
        return cldContractMapper.selectCldContractByContractId(contractId);
    }

    /**
     * 查询合同
     *
     * @param projectId 合同主键
     * @return 合同
     */
    @Override
    public CldContract selectCldContractByProjectId(Long projectId) {
        return cldContractMapper.selectCldContractByProjectId(projectId);
    }

    /**
     * 查询合同列表
     *
     * @param cldContract 合同
     * @return 合同
     */
    @Override
    public List<CldContract> selectCldContractList(CldContract cldContract) {
        return cldContractMapper.selectCldContractList(cldContract);
    }

    /**
     * 新增合同
     *
     * @param cldContract 合同
     * @return 结果
     */
    @Override
    public int insertCldContract(CldContract cldContract) {
        cldContract.setCreateTime(DateUtils.getNowDate());
        cldContract.setUpdateTime(cldContract.getCreateTime());
        cldContract.setCreateId(SecurityUtils.getUserId());
        cldContract.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        cldContract.setCreateName(SecurityUtils.getUsername());
        cldContract.setFlag(0L);
        cldContract.setContractStatus(1L);
        cldContract.setContractNo("HT" + DateUtils.dateTimeNow("yyyyMMdd") + redisService.getDailyCount());
        //修改項目狀態為合同签订
        CldProject cldProject = cldProjectMapper.selectCldProjectByProjectId(cldContract.getProjectId());
        if (cldProject != null) {
            //判断项目是否为未签订状态
            if (cldProject.getProjectStatus() != null && cldProject.getProjectStatus() == 1) {
                cldProject.setProjectStatus(2L);//更新为签订状态
                cldProject.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
                cldProject.setUpdateTime(cldContract.getUpdateTime());
                cldProjectMapper.updateCldProject(cldProject);
            }
        }


        return cldContractMapper.insertCldContract(cldContract);
    }

    /**
     * 修改合同
     *
     * @param cldContract 合同
     * @return 结果
     */
    @Override
    public int updateCldContract(CldContract cldContract) {
        return cldContractMapper.updateCldContract(cldContract);
    }

    /**
     * 批量删除合同
     *
     * @param contractIds 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteCldContractByContractIds(Long[] contractIds) {
        return cldContractMapper.deleteCldContractByContractIds(contractIds);
    }

    /**
     * 删除合同信息
     *
     * @param contractId 合同主键
     * @return 结果
     */
    @Override
    public int deleteCldContractByContractId(Long contractId) {
        return cldContractMapper.deleteCldContractByContractId(contractId);
    }
}
